<template>
<div id="login">
  <el-card class="login-card">
    <div slot="header">
      <span>二手店登陆</span>
    </div>
    <el-form ref="form" :model="user" :rules="rules" label-width="80px">
      <el-form-item label="账号" prop="account">
        <el-input v-model="user.account"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="margin-left: -80px;" @click="login">登陆 | 注册</el-button>
        <router-link to="goods-list" tag="a" style="margin-left:30px">首页</router-link>
      </el-form-item>
    </el-form>
  </el-card>
</div>
</template>

<script>
import http from '@/components/service/http.js'
import {
  mapGetters
} from 'vuex'

export default {
  name: 'login',
  data() {
    return {
      user: {
        account: '',
        password: ''
      },
      rules: {
        account: [{
          required: true,
          message: '请输入账号',
          trigger: 'blur'
        }],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }]
      }
    }
  },
  created() {},
  methods: {
    login: function () {
      var This = this;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          http.post('/shs/user/login', this.user)
            .then(function (response) {
              var res = response.data;
              if (res.status == 'success') {
                This.$store.commit('setUserId', res.data.id);
                This.$router.push({
                  name: 'goods-list'
                });
              }
            })
        }
      });
    }
  }
}
</script>

<style scoped>
#login {}

#login .login-card {
  width: 500px;
  margin: auto;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  height: 280px;
}
</style>
